﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using filmquiz.Src.Login;
using System.Web.SessionState;
using filmquiz.Src.Domain;
using filmquiz.Src.DatabaseAccess;

namespace filmquiz.Src.SessionHandle
{
    public class SessionController
    {
        private LoginController loginController;
        private DbAcces dbAccess;
        private UserHelper userHelper;

        public SessionController()
        {
            loginController = new LoginController();
            dbAccess = DbAcces.getInstance();
            userHelper = UserHelper.getInstance();
        }

        public User LoginUser(String username, String password, HttpSessionState session)
        {
            User user = null;
            if (loginController.DoLogin(username, password))
            {
                user = userHelper.getUser(username);
                updateSession(user, session);
            }
            else
            {
                //TODO Bad user or password message.
            }
            return user;
        }

        public void logoutUser(HttpSessionState session)
        {
            session["loggedIn"] = false;
            session["user"] = null;
            session["image"] = null;
        }

        public void updateSession(User currentUser, HttpSessionState session)
        {
            session["loggedIn"] = true;
            session["user"] = currentUser;
        }
    }
}